import xml from '@ohos.xml';
import axios from '@ohos/axios';
import logger from '../core/logger';
import apiInfo from './core/apiInfo';
import apiProvider from './core/apiProvider';
import { clientBase } from './core/clientBase';
import { API_USER_AGENT, HOST } from '../core/dict';
import { emptyData } from '../model/common/emptyData';
import returnModel from '../model/common/returnModel';
import danmakuModel from '../model/danmaku/danmakuModel';

class danmakuClient extends clientBase {
  constructor() {
    super();
  }

  async getDanmakuData(cid: number, type: number, index: number):Promise<Uint8Array>{
    let res = await axios({
      method: "get",
      url: 'https://api.bilibili.com/x/v2/dm/list/seg.so',
      headers: {
        "referer": HOST,
        "app-key": "android64",
        "user-agent": API_USER_AGENT,
        "x-bili-aurora-zone": "sh001",
        "x-bili-aurora-eid": "UlMFQVcABlAH",
        "content-type": 'application/octet-stream'
      },
      params: {
        oid: cid,
        type: type,
        segment_index: index
      }
    });
    return  new Uint8Array(res.data);
  }

  getBiliBili(cid: number, segmentIndex?: number): Promise<returnModel<string>> {
    return new Promise((resolve) => {
      try {
        let url = '';
        if (segmentIndex) {
          url = `http://api.bilibili.com/x/v2/dm/list/seg.so?type=1&oid=${cid}&segment_index=${segmentIndex}`;
        } else {
          url = `https://api.bilibili.com/x/v1/dm/list.so?oid=${cid}`;
        }
        this.client.getResultXml(url).then((res) => {
          let data: string = res;
          resolve(this.buildSuccessResult<string>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<string>(e));
      }
    });
  }

  send(aId: string, cid: string, modeInt: number, color: number, text: string, position: number): Promise<returnModel<emptyData>> {
    return new Promise(async (resolve) => {
      try {
        let info = new apiInfo("https://api.bilibili.com/x/v2/dm/post", `&aid=${aId}&oid=${cid}`, true);
        let url = await info.toString();
        let data = `pool=0&rnd=${apiProvider.timeSpanSeconds}&oid=${cid}&fontsize=25&msg=${encodeURIComponent(text)}&mode=${modeInt}&progress=${position}&color=${color}&plat=2&screen_state=0&from=861&type=1`;
        this.client.postResult(url, data).then((res) => {
          let data: emptyData = res.data;
          resolve(this.buildSuccessResult<emptyData>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }

  parseBiliBiliXml(strXml: string): Promise<Array<danmakuModel>> {
    let array = [];
    return new Promise((resolve) => {
      try {
        let arrayBuffer = new ArrayBuffer(strXml.length);
        let bufView = new Uint8Array(arrayBuffer);
        let strLen = strXml.length;
        for (let tmp = 0; tmp < strLen; ++tmp)
          bufView[tmp] = strXml.charCodeAt(tmp);
        let that = new xml.XmlPullParser(arrayBuffer);

      } catch (e) {
        logger.error(e);
      }
      resolve(array);
    });
  }
}
export default new danmakuClient();